package wnhz.com.business.shareshop;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.shareshop.fragment.ClassifyFragment;
import wnhz.com.business.shareshop.fragment.HomeFragment;
import wnhz.com.business.shareshop.fragment.MyFragment;
import wnhz.com.business.shareshop.fragment.ShoppingCartFragment;

/**
 * Created by Administrator on 2017-12-13 0013.
 * 共享商城的界面的入口
 */

public class ShareShopActivity extends MyBaseActivity implements View.OnClickListener {


    @BindView(R.id.main_freecontent_f)
    FrameLayout mainFreecontentF;
    @BindView(R.id.main_tabhost_f)
    FragmentTabHost mainTabhostF;
    private TextView mTvNumInCart;

    private int tab_fragment;//跳转到指定fragment

    public static Intent createIntent(Context context, int tab_fragment) {
        Intent intent = new Intent(context, ShareShopActivity.class);
        intent.putExtra("tab_fragment", tab_fragment);//跳转到指定fragment

        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        tab_fragment = intent.getIntExtra("tab_fragment", 0);
    }

    /**
     * 带状态选择器的图标
     */
    private int[] mTabSelectors = new int[]{R.drawable.bg_rbtn_1, R.drawable.bg_rbtn_2, R.drawable.bg_rbtn_3, R.drawable.bg_rbtn_5};
    private Class[] mFragments_f = new Class[]{HomeFragment.class, ClassifyFragment.class, ShoppingCartFragment.class, MyFragment.class};
    private String[] tabname_f = new String[]{"首页", "分类", "购物车", "我的"};

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_shop);
        ButterKnife.bind(this);
        parseIntent();
        mainTabhostF.setup(this, getSupportFragmentManager(), R.id.main_freecontent_f);
        addTab();
    }

    private void addTab() {
        for (int i = 0; i < mFragments_f.length; i++) {
            View tabIndicator = getLayoutInflater().inflate(R.layout.rebate_indicator, null);//底部菜单布局
            ImageView imageView = (ImageView) tabIndicator.findViewById(R.id.imageView1);
            imageView.setImageResource(mTabSelectors[i]);
            TextView textView = (TextView) tabIndicator.findViewById(R.id.tv_names);
            textView.setText(tabname_f[i]);
            if (i == 0) {
                textView.setTextColor(getResources().getColor(R.color.bg_main));
            }

            if (i == 2) {
                mTvNumInCart = (TextView) tabIndicator.findViewById(R.id.textView1);//右上角数量
            }

            mainTabhostF.addTab(mainTabhostF.newTabSpec(tabname_f[i]).setIndicator(tabIndicator), mFragments_f[i], null);
            tabIndicator.setId(i);
            tabIndicator.setOnClickListener(this);
        }

        //文字选中变化
        mainTabhostF.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                for (int i = 0; i < mainTabhostF.getTabWidget().getChildCount(); i++) {
                    TextView tv = (TextView) mainTabhostF.getTabWidget().getChildAt(i).findViewById(R.id.tv_names);
                    if (mainTabhostF.getCurrentTab() == i) {//选中
                        tv.setTextColor(getResources().getColor(R.color.red255));
                    } else {//不选中
                        tv.setTextColor(getResources().getColor(R.color.text51));
                    }
                }

            }
        });

        if(-1 != tab_fragment){
            setCurrentTabByTag(tab_fragment);//跳转到指定fragment
        }
    }
    private long mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                MyToast("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                MyApplication.getInstance().exit(this);
                finish();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case 0:
                setCurrentTabByTag(0);
                break;
            case 1:
                setCurrentTabByTag(1);
                break;
            case 2:
                setCurrentTabByTag(2);
                break;
            case 3:
                setCurrentTabByTag(3);
                break;
        }
    }


    public void setCurrentTabByTag(int currentTabByTag) {
        mainTabhostF.setCurrentTabByTag(tabname_f[currentTabByTag]);
    }
}
